package cn.zzzhy.spider;

import cn.zzzhy.entity.URL;
import cn.zzzhy.util.HttpClientUtil;
import cn.zzzhy.util.URLParser;
import com.google.common.base.Strings;

/**
 * @author zzzhy
 * @since 16/2/27 下午6:06
 */
public class PageSpider implements Runnable {

    private RunSwitch run = RunSwitch.run;
    private Thread thread = null;

    private enum RunSwitch {
        run, pause, stop
    }

    public PageSpider() {
    }

    //开始去取网页
    public void fetch() {
        if (thread == null) {
            thread = new Thread(this);
            thread.start();
        } else {
            SpiderPool.remaining.notifyAll();
        }
    }

    public void stop() {
        run = RunSwitch.stop;
        if (thread != null) {
//            SpiderPool.remaining.notifyAll();
            thread = null;
        }
    }

    public void pause() {
        run = RunSwitch.pause;
    }

    @Override
    public void run() {
        boolean isRun = true;
        while (isRun) {
            String s;
            synchronized (SpiderPool.remaining) {
                while (SpiderPool.remaining.isEmpty()) {
                    try {
                        SpiderPool.remaining.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                s = SpiderPool.remaining.iterator().next();
                SpiderPool.remaining.remove(s);
                SpiderManager.client.getOutText().append(thread.getName() + " --- handling url --- " + s + "\r\n");
                URL url = URLParser.parseUrl(s);
                if (url == null) {
                    continue;
                }
                while (run == RunSwitch.pause) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (run == RunSwitch.stop) {
                    isRun = false;
                } else {
                    String pageContent = HttpClientUtil.connect(s, url.isHttps());
//                        SpiderManager.client.getOutText().append(url.getLocation() + " 的返回:\t"+pageContent.length()+"\r\n");
                    if (!Strings.isNullOrEmpty(pageContent)) {
                        URLParser.parse(pageContent);
                    }
                    SpiderPool.remaining.remove(s);
                    SpiderPool.visitedURLs.put(s, url);
                }
                SpiderPool.remaining.notifyAll();
            }

        }
    }
}
